package org.gordonframework.site.transitions {
	import org.gordonframework.core.events.GProcessEvent;
	import org.gordonframework.core.process.GProcess;
	import org.gordonframework.site.events.GTransitionEvent;
	import org.gordonframework.site.views.GView;

	import flash.events.IEventDispatcher;	

	/**
	 * @author sampet
	 */
	public class GTransition extends GView {

		protected var durationMultiplier:Number;
		protected var container:GView;

		protected var views:Array;

		private var _newView:GView;		private var _oldView:GView;
		private var _currentView:GView;

		private var _loader:GProcess;

		public function GTransition() {
			durationMultiplier = 1;
			container = new GView( );
			views = new Array( );
						
			addChild( container );
		}

		public function get loader():IEventDispatcher {
			return _loader;
		}

		public function get newView():GView {
			return _newView;
		}

		public function get oldView():GView {
			return _oldView;
		}

		public function get currentView():GView {
			return _currentView;
		}

		public function get numViews():int {
			return views.length;
		}

		public function addView(view:GView):void {
			views.push( view );
			container.addChild( view );
			view.visible = false;
		}

		public function removeView(view:GView):void {
			var index:int = views.indexOf( view );
			if(index > -1) {
				views.splice( index, 1 );
			}
			if(container.contains( view )) {
				container.removeChild( view );
			}
		}	

		public function getViewAt(index:int):GView {
			return views[index] as GView;
		}

		public function getViewByName(name:String):GView {
			for each(var view:GView in views) {
				if(view.name == name) {
					return view;
				}
			}
			return null;
		}

		public function getViewIndex(view:GView):int {
			return views.indexOf( view );
		}

		public function next():void {
			var currentIndex:int = getViewIndex( currentView );
			if(currentIndex > numViews - 2) {
				goto( getViewAt( 0 ) );
			} else {	
				goto( getViewAt( currentIndex + 1 ) );	
			}
		}

		public function prev():void {
			var currentIndex:int = getViewIndex( currentView );
			if(currentIndex - 1 < 0) {
				goto( getViewAt( numViews - 1 ) );
			} else {
				goto( getViewAt( currentIndex - 1 ) );	
			}					
		}

		public function goto(view:GView, fastForward:Boolean = false):void {
			if(!_newView || _newView != view || durationMultiplier == 1 && !fastForward) {
				_newView = view;
				dispatchEvent( new GTransitionEvent( GTransitionEvent.TRANSITION_START ) );
				//if(_loader.running) {
				//	_loader.stop( );
				//}
				durationMultiplier = fastForward ? 0 : 1;
				hideView( );
			}
		}

		protected function hideView():void {
			currentView.visible = false;
			hideViewComplete( );
		}

		protected function hideViewComplete():void {
			if(newView) {
				if(!container.contains( newView )) {
					addView( newView );
				}
				/*
				var initProcess:GProcess = newView.getInitProcess( );
				if(initProcess && !initProcess.complete) {
				initProcess.start( );
				} else {
				loaderCompleteHandler( );	
				}
				 */
				loaderCompleteHandler( );
			} else {
				_oldView = _currentView;
				_currentView = _newView;
				dispatchEvent( new GTransitionEvent( GTransitionEvent.TRANSITION_END ) );
			}
		}

		private function loaderCompleteHandler(event:GProcessEvent = null):void {
			_oldView = _currentView;
			_currentView = _newView;
			showView( );
		}

		protected function showView():void {
			currentView.visible = true;
			showViewComplete( );
		}

		protected function showViewComplete():void {
			dispatchEvent( new GTransitionEvent( GTransitionEvent.TRANSITION_END ) );						
		}
	}
}
